package com.chance.cc.crawler.development.command.job.domain.xhs.applets;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.development.scripts.xiaohongshu.applets.XHSAppletsArticleCrawlerScript;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerIP;
import static com.chance.cc.crawler.development.command.publish.douyin.DYPublishCrawlerJob.metaServerPort;

/**
 * @author bx
 * @date 2020/12/6 0006 12:44
 */
public class XHSBDArticleCrawlerScheduleJob {

    public static String domainId = "xhs";
    private static String startUrl = XHSAppletsArticleCrawlerScript.startListUrl;
    private static CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(metaServerIP,metaServerPort);
    public static String articleQueue = StringUtils.joinWith("-","crawler",domainId,XHSAppletsArticleCrawlerScript.site,"queue");
    public static String listQueueName = StringUtils.joinWith("-","crawler",domainId,"xhs-time-list","queue");
    public static String articleAccumQueue = StringUtils.joinWith("-",  domainId,"article","accumulate", "queue");
    public static String articleTimeQueue = StringUtils.joinWith("-",  domainId,"article","time", "queue");
    public static String articleIdsQueue = StringUtils.joinWith("-",  domainId,"article","ids", "queue");
    private static final String metaServer = "http://192.168.1.215:9599";
//    private static final String metaServer = "http://localhost:9599";


    public static void main(String[] args) {

        publishSearchCrawlerScheduleJobInfo();
        System.out.println(crawlerSchduler().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishSearchCrawlerScheduleJobInfo(){


        CrawlerJob crawlerSchduler =  crawlerSchduler();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domainId);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerSchduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("小红书关键词搜索定时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerSchduler.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布crawler作业：" + page.getRawText());
        return crawlerSchduler;

    }

    /**
     * dy采集
     * @return
     */
    public static CrawlerJob crawlerSchduler() {
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domainId, turnPage)
                .httpUrl(startUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .build();
        requestRecord.getHttpConfig().setResponseTextGenerateHtml(false);
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);

        requestRecord.tagsCreator().bizTags().addDomain(domainId);
        requestRecord.tagsCreator().bizTags().addSite(XHSAppletsArticleCrawlerScript.site);


        CrawlerQueueConfig articleAccumulateQueueConfig = CrawlerMetaConstant.redisRequestQueue(articleAccumQueue);
        articleAccumulateQueueConfig.setQueueContent(CrawlerQueueConfig.Content.result);
        articleAccumulateQueueConfig.setQueueStructure(CrawlerQueueConfig.Structure.map);

        CrawlerQueueConfig articleTimeQueueConfig = CrawlerMetaConstant.redisRequestQueue(articleTimeQueue);
        articleTimeQueueConfig.setQueueContent(CrawlerQueueConfig.Content.result);
        articleTimeQueueConfig.setQueueStructure(CrawlerQueueConfig.Structure.map);

        CrawlerQueueConfig articleIdsQueueConfig = new CrawlerQueueConfig(
                articleIdsQueue,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list
        );

        String initUrl = metaServer + "/crawler/domain/common/api/v1/xhs/article/queue/init?" +
                "crawlerArticleQueueName=%s&articleTimeQueueName=%s&crawlerListQueueName=%s";
        CrawlerRequestRecord initArticleRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xhs_article_init", turnPageItem)
                .httpUrl(String.format(initUrl,articleQueue,articleTimeQueue,listQueueName))
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(
                        domainId,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domainId, XHSAppletsArticleCrawlerScript.site, CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerJobThreadNumber(3)
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(articleQueue))
//                .fileResultPipeline("article_result", "D:\\chance\\log\\xhs\\xhs.log", false)
                .kafkaResultPipeline("article_result","tmp_social_media",null)
                .queueResultPipeline("article_accumulate",articleAccumulateQueueConfig)
                .queueResultPipeline("article_ids",articleIdsQueueConfig)
                .queueResultPipeline("article_time",articleTimeQueueConfig)
                .requestRecord(requestRecord)
                .supportRecord(initArticleRecord)
                .restWhileNon(false)
                .build();
    }

}
